package com.niit.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MusicService extends Service implements MediaPlayer.OnCompletionListener{
    private MediaPlayer player;

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.yezetian);
        player.setOnCompletionListener(this);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(player == null) {
            player = MediaPlayer.create(this, R.raw.yezetian);
        }
        int status = intent.getIntExtra("status", 0);
        switch (status) {
            case 0:
                player = MediaPlayer.create(this, R.raw.yezetian);
                player.start();
                break;
            case 1:
                if(player.isPlaying()) {
                    player.pause();
                }
                break;
            case 2:
                if(!player.isPlaying()) {
                    player.start();
                }
                break;
            case 3:
                if(player.isPlaying()) {
                    player.stop();
                    player.release();
                    player = null;
                }
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        if (player != null) {
            player.release();//player释放解除
        }
        super.onDestroy();
    }
    @Override
    public void onCompletion(MediaPlayer mp) {
        mp.release();
    }
}
